<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>选择分类</title>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/common/table.css">
    <%@include file="../../include/commonFile.jsp" %>
    <style type="text/css">
        .index-outside > section .section-main {
            min-width: 0px !important;
            /*max-width: 800px !important;*/
            padding: 0px 15px;
        }

        .index-outside > section {
            padding-bottom: 0px !important;
        }

        .index-outside > section .section-main {
            margin-bottom: 0px;
        }

        .expand .icon-right {
            -webkit-transform: rotate(90deg);
            -moz-transform: rotate(90deg);
            -ms-transform: rotate(90deg);
            transform: rotate(90deg);
        }

        .icon-right {
            display: inline-block;
            -webkit-transition: all .5s;
        }
    </style>
</head>
<body>
<!--头部-->
<div class="index-outside">
    <!--内容-->
    <section class="no-fix">
        <div class="section-main">
            <c:if test="${empty isMulti}">
            <div class="layui-tab-item layui-show">
                <div class="layui-tab layui-tab-brief" lay-filter="type">
                    <ul class="layui-tab-title" id="zc_tab">
                        <li>已上架商品</li>
                        <li class="layui-this">商品分类</li>
                    </ul>
            </c:if>
                    <form class="layui-form" action="${ctx}/mall/category/selectCategory.do" id="myForm"
                          method="post">
                        <input type="hidden" name="pageNo" id="pageNo"/>
                        <input type="hidden" name="goodsId" value="${goodsId}"/>
                        <input type="hidden" name="categoryIds" value="${categoryIds}"/>
                        <input type="hidden" name="isMulti" value="${isMulti}" />
                        <%--<div class="f-search-bar">
                            <div class="search-container">
                                <ul class="search-form-content">
                                    <li class="form-item-inline"><label class="search-form-lable">名称</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="name" autocomplete="off" class="layui-input"
                                                   value="${mallCategory.name}" placeholder="请输入查询类型名称">
                                        </div>
                                    </li>
                                    <li class="form-item-inline">
                                        <div class="sub-btns">
                                            <a class="layui-btn layui-btn-danger"
                                               href="javascript:submitFunction('#myForm')">查询</a>
                                            <a class="layui-btn layui-btn-normal"
                                               href="javascript:resetFunction('#myForm')">重置</a>
                                        </div>
                                    </li>
                                </ul>
                            </div>
                        </div>--%>
                    </form>
                    <div class="c-time-list-content" style="padding-top: 0px;">
                        <input type="hidden" id="categoryIds"/>
                        <input type="hidden" id="categoryNames"/>
                        <input type="hidden" id="selectDatas"/>
                        <table class="layui-table layui-form" lay-skin="line">
                            <colgroup>
                                <col>
                                <col width="150">
                                <%--<col width="150">--%>
                            </colgroup>
                            <thead>
                            <tr>
                                <th></th>
                                <th>商品数量</th>
                                <%--<th>创建时间</th>--%>
                            </tr>
                            </thead>
                            <tbody>
                            <c:forEach var="category" items="${list}">
                                <tr data-id="${category.id}">
                                    <td>
                                        <span>
                                            <!-- 商品及分类 -->
                                            <c:if test="${empty isMulti}">
                                                ${category.name}
                                            </c:if>
                                            <!-- 分类导航 -->
                                            <c:if test="${not empty isMulti}">
                                                <c:if test="${isMulti == 1}">
                                                    <input name="parentId" lay-filter="parentRadio" type="radio" value="${category.id}" title="${category.name}"/>
                                                </c:if>
                                                <c:if test="${isMulti == 2}">
                                                    ${category.name}
                                                </c:if>
                                            </c:if>
                                        </span>&emsp;
                                        <c:if test="${fn:length(category.childrens) > 0}">
                                            <span class="expand"><i class="iconfont icon-right"></i></span>
                                        </c:if>
                                    </td>
                                    <td></td>
                                    <%--<td><fmt:formatDate value="${category.createDate}" pattern="yyyy-MM-dd HH:mm"/></td>--%>
                                </tr>
                                <c:if test="${fn:length(category.childrens) > 0}">
                                    <c:forEach var="children" items="${category.childrens}">
                                        <tr class="children-tr-${category.id}" data-pName="${category.name}" data-name="${children.name}"
                                            data-id="${children.id}" data-pic="${children.pic}">
                                            <c:set var="chk" value=""/>
                                            <c:forEach var="categoryId" items="${fn:split(categoryIds, ',')}">
                                                <c:if test="${children.id == categoryId}">
                                                    <c:set var="chk" value="true"/>
                                                </c:if>
                                            </c:forEach>
                                            <td>
                                                <span>
                                                    <c:if test="${empty isMulti}">
                                                        <input name="id" type="radio" ${chk == true ? 'checked="checked"' : ''}
                                                               title="${children.name}" value="${children.id}"
                                                        />
                                                    </c:if>
                                                    <c:if test="${not empty isMulti}">
                                                        <input name="id" type="checkbox" ${chk == true ? 'checked="checked"' : ''}
                                                               title="${children.name}" lay-skin="primary" data-name="${children.name}"
                                                               value="${children.id}" lay-filter="categoryChk"/>
                                                    </c:if>
                                                </span>
                                            </td>
                                            <td>${children.goodsNum}</td>
                                            <%--<td><fmt:formatDate value="${category.createDate}" pattern="yyyy-MM-dd HH:mm"/></td>--%>
                                        </tr>
                                    </c:forEach>
                                </c:if>
                            </c:forEach>
                            </tbody>
                        </table>
                        <div id="page_content" class="page-container"></div>
                    </div>
        <c:if test="${empty isGoods}">
                </div>
            </div>
        </c:if>
        </div>
    </section>
</div>
<!--底部-->
<%@include file="../../include/incScript.jsp" %>
<script type="text/javascript" src="${ctx}/script/common/list.js"></script>
<script type="text/javascript">
    var storage = localStorage;
    $(function () {
        storage.setItem("myCategory", '${categoryJson}');
        // 加载分页
        loadPageNew({
            elem: 'page_content',
            totalCount: '${page.totalCount}',
            limit: '${page.limit}',
            page: '${page.page}'
        });
        layui.use(['form', 'laypage', 'element'], function () {
            var form = layui.form, element = layui.element;

            // 一级分类选择
            form.on('radio(parentRadio)', function (data) {
                var $target = $(data.elem);
                var parentId = $target.closest("tr").data("id");
                var checked = data.elem.checked;

                $("tbody tr[class*=children-tr]").each(function (jj, ele) {
                    $(ele).find("[type=checkbox]").prop("checked", false);
                });

                $("tr[class=children-tr-" + parentId + "]").each(function (index, ele) {
                    if (checked) {
                        $(ele).find("[type=checkbox]").prop("checked", true);
                    } else {
                        $(ele).find("[type=checkbox]").prop("checked", false);
                    }
                });
                form.render('checkbox');
            });

            if (util.isValid('${isMulti}') && '${isMulti}' == 2) {
                // 二级分类选择
                form.on('checkbox(categoryChk)', function (data) {
                    var giftJson = storage.getItem("myCategory");
                    var _giftArray = eval(giftJson);

                    var index = -1; // 如果不存在返回-1，否则返回index
                    $.each(_giftArray, function (ii, dd) {
                        if (data.value == dd.id) {
                            index = ii;
                        }
                    });

                    var chk = $(data.elem)[0].checked; // 是否选中
                    if (chk) {
                        if (index == -1) {
                            $("tbody tr[class*=children-tr]").each(function (jj, dd) {
                                var id = $(dd).data("id");
                                if (data.value == id) {
                                    var obj = {
                                        id: id,
                                        name: $(dd).data("name")
                                    };
                                    _giftArray.push(obj);
                                }
                            });
                        }
                    } else {
                        _giftArray.splice(index, 1);
                    }

                    var _idsArray = _giftArray.filter(function (arr) {
                        return true;
                    }).map(function (arr) {
                        return arr.id;
                    });

                    storage.setItem("myCategory", JSON.stringify(_giftArray));
                    $("[name=categoryIds]").val(_idsArray.join(","));
                });
            }

            element.on('tab(type)', function (data) {
                if (data.index == 0) {
                    location.href = "${ctx}/mall/goods/selectGoods.do?isMulti=${isMulti}";
                } else if (data.index == 1) {
                    location.href = "${ctx}/mall/category/selectCategory.do?isMulti=${isMulti}";
                }
            });
        });
        $("#submitBtn").click(function () {
            $("#myForm").submit();
        });

        $("#resetBtn").click(function () {
            $("#myForm input[type=text]").val("");
        });

        $("tbody").delegate(".icon-right", "click", function (e) {
            var $target = $(e.target);
            var $span = $target.parent('span');
            var parentId = $target.closest("tr").data("id");
            if ($span.hasClass("expand")) {
                $span.removeClass("expand");
                $("tr[class=children-tr-" + parentId + "]").hide();
            } else {
                $span.addClass("expand");
                $("tr[class=children-tr-" + parentId + "]").show();
            }
        })
    });

    // 商品和分类的弹窗 单选
    function singleSelect() {
        var length = $("[name=id]:checked").length;
        if (length == 0) {
            util.layerMsgError("请先选择");
            return false;
        }
        var _result = new Array();
        $("[name=id]:checked").each(function (index, ele) {
            var $tr = $(ele).closest("tr");
            var map = {
                targetId: $tr.data("id"),
                name: $tr.data("name"),
                pic: $tr.data("pic"),
                pName : $tr.attr("data-pName"),
                type: 3
            };
            _result.push(map);
        });
        $("#selectDatas").val(JSON.stringify(_result));
        return true;
    }

    // 多选
    function multiSelect() {
        var giftJson = storage.getItem("myCategory");
        var _giftArray = eval(giftJson);
        if (_giftArray.length == 0) {
            util.layerMsgError("请先选择")
            return false;
        }
        var flag = true;
        var _idArray = new Array();
        var _nameArray = new Array();

        $.each(_giftArray, function (ii, dd) {
            _idArray.push(dd.id);
            _nameArray.push(dd.name);
        });

        var ids = _idArray.join(",");
        var names = _nameArray.join(",");
        $("#categoryIds").val(ids);
        $("#categoryNames").val(names);
        return flag;
    }

    function doSubmit() {
        var isMulti = '${isMulti}';
        if (util.isValid(isMulti)) {
            if (isMulti == 1) {   // 纯分类
                return singleSelect();
            } else if (isMulti == 2) { // 商品选择分类
                return multiSelect();
            }
        } else { // 选择商品及分类
            return singleSelect();
        }
    }
</script>
</body>
</html>